//
//  ZHTextField.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/11.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

enum ZHTextFieldStyle {
    case `default`
    case number
}
class ZHTextField: NSView {
   
    var isBecomeFirst:Bool = false {
        didSet {
            let borderColor = isBecomeFirst ? color_main108:color_gray(200)
            self.view.layer?.borderColor = borderColor.cgColor
            
            self.view.layer?.borderWidth = isBecomeFirst ? 1:1
            //self.boardView.isHidden = !isBecomeFirst
            if self.textField.stringValue.count != 0 {
                self.tipLab.isHidden = !isBecomeFirst
            }else {
                self.tipLab.isHidden = true
            }
            
        }
    }
    
    
    var title:String = "..." {
        didSet {
            self.tipLab.stringValue = title
        }
    }
    
    
    var placeholderAttributedString:NSAttributedString? {
        didSet {
            self.textField.zh_cell.placeholderAttributedString = placeholderAttributedString
        }
    }
    var placeholderString:String? {
        didSet {
            self.textField.zh_cell.placeholderString = placeholderString
        }
    }
    
    var radius:CGFloat = 0 {
        didSet {
            self.view.zh_border(radius: radius, borderColor: boardColor, borderWidth: boardWidth)
        }
    }
    
    var boardColor:NSColor = NSColor.black {
        didSet {
            self.view.zh_border(radius: radius, borderColor: boardColor, borderWidth: boardWidth)
        }
    }
    
    var boardWidth:CGFloat = 0.5 {
        didSet {
            self.view.zh_border(radius: radius, borderColor: boardColor, borderWidth: boardWidth)
        }
    }
    
    var isOnlyNumber:Bool = false {
        didSet {
            if isOnlyNumber == true {
                self.textField.formatter = IntegerValueFormatter()
            }else {
                self.textField.formatter = Formatter.init()
            }
            
        }
    }
    
    var style:ZHTextFieldStyle = .default {
        didSet {
            switch style {
            case .number:
                self.textField.formatter = IntegerValueFormatter()
            default:
                self.textField.formatter = Formatter.init()
            }
        }
    }
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.zh_setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func zh_setupUI() {
        
        self.addSubview(view)
        view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(10, 0, 0, 0))
        }
        
        self.addSubview(tipLab)
        tipLab.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.top.equalToSuperview()
            make.left.equalTo(10)
        }
        
        self.addSubview(boardView)
        boardView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(3, 3, 3, 3))
        }
        
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(20)
            make.centerY.equalToSuperview().offset(5)
        }
        
        self.boardColor = color_gray(200)
        self.boardWidth = 0.5
        self.radius = 2
    }
    
    lazy var view: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var boardView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.cornerRadius = 0
        view.layer?.borderColor = color_main108.cgColor
        view.layer?.borderWidth = 1
        view.isHidden = true
        return view
    }()
    
    lazy var tipLab: NSLabel = {
        let lab = NSLabel.label(title: "您的姓名", textColor: color_main108, font: NSFont.systemFont(ofSize: 13))
        lab.wantsLayer = true
        lab.layer?.backgroundColor = color_gray(255).cgColor
        lab.sizeToFit()
        lab.isHidden = true
        return lab
    }()
    
    lazy var textField: BaseTextField = {
        let tef = BaseTextField.init(frame: NSRect.zero)
        return tef
    }()
    
}
